<template>
  <div class="card-container default-card" :style="{ width }">
    <el-card v-bind="$attrs">
      <!-- card header -->
      <div slot="header" class="card-header">
        <h3 class="card-title">{{ title }}</h3>
        <el-button type="text" v-if="hasBtn" @click="backHandler">
          {{ btnText }}
        </el-button>
      </div>

      <!-- card body -->
      <slot></slot>
    </el-card>
  </div>
</template>

<script>
export default {
  name: "card",
  props: {
    width: {
      type: String,
      default: "500px", // min-width: 500px
    },
    title: {
      type: String,
      default: "登录",
    },
    hasBtn: {
      type: Boolean,
      default: true,
    },
    btnText: {
      type: String,
      default: "返回",
    },
    onBack: {
      type: [Function, null],
      default: null,
    },
  },
  methods: {
    // 点击返回按钮
    backHandler() {
      if (this.onBack && typeof this.onBack === "function") {
        this.onBack();
        return;
      }
      this.$router.back();
    },
  },
};
</script>

<style lang="scss">
.card-container {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  &.default-card {
    width: 500px;
    min-width: 500px;
  }

  .el-card__header {
    padding: 10px 20px;
  }

  .card-header {
    display: flex;
    justify-content: space-between;
    .card-title {
      flex: 1;
      display: flex;
      align-items: center;
    }
  }
}
</style>
